package com.example.ws;

import com.example.ws.handler.elog.ELogHandler;
import com.example.ws.handler.elog.ELogService;
import com.example.ws.handler.elsearch.ElSearchClient;
import com.example.ws.handler.erole.ERoleHandler;
import com.example.ws.handler.test.TestHandler;
import com.example.ws.util.Container;
import io.javalin.Javalin;

import java.io.IOException;
import java.util.Properties;

import static io.javalin.apibuilder.ApiBuilder.path;

// @Slf4j
public class WsApplication {
	

	public static void main(String[] args) {
		Properties configProp = null;
		try {
			configProp = GlobalConfLoader.load();
		} catch (IOException e) {
			System.out.println("配置加载失败.....");
			System.exit(-1);
		}
		registerSingleService();
		var app = Javalin.create(config -> {
			config.useVirtualThreads = true;
			config.http.asyncTimeout = 10_000L;
			config.http.maxRequestSize = 10 * 1024 * 1024;
			
			config.bundledPlugins.enableCors(cors -> cors.addRule(it -> {
                // it.allowHost("http://localhost:8006");
                it.allowCredentials = true;
                it.reflectClientOrigin = true;
            }));
			// config.staticFiles.add("/src/main/resources");
			// config.staticFiles.add("/public");
			config.router.apiBuilder(() -> {
				path("/test", 					new TestHandler());
				path("/elog/console", 			new ELogHandler());
				path("/erole", 					new ERoleHandler());
			});
		}).start(Integer.parseInt(configProp.getProperty(GlobalConfLoader.CONFIG_KEY_SERVER_PORT)));
		
		// 异常处理
		app.exception(Exception.class, (e, ctx) -> e.printStackTrace());
	}

	public static void registerSingleService() {

		// ==================== service static obj ====================
		// Container.getInstance().putObject(ELogService.instance);
		// Container.getInstance().putObject(ELogService.instance);

		// ==================== other static obj ====================
		// Container.getInstance().putObject(new ElSearchClient());
	}

}
